//
//  CRSpecialNewsViewController.m
//  NewAge
//
//  Created by dllo on 15/12/1.
//  Copyright © 2015年 ColorRevolution. All rights reserved.
//

#import "CRSpecialNewsViewController.h"
#import "CRSpecialNewsTitleModel.h"
#import "CRSpecialNewsModel.h"
#import "CRSpecialSavaModel.h"
#import "CRNewsTableViewCell.h"
#import "CRPicTableViewCell.h"
#import "CRSpecialNewsTableViewCell.h"
#import "CRNewsModel.h"
#import "CRNewsDetailViewController.h"
#import "CRPicNewsViewController.h"

@interface CRSpecialNewsViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *specialArr;

@property (nonatomic, strong) CRSpecialSavaModel *sava;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *headBgView;

@property (nonatomic, strong) UIImageView *headImageView;

@property (nonatomic, strong) UIView *headTitleView;

@end

@implementation CRSpecialNewsViewController

#pragma mark - init

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.specialArr = [NSMutableArray array];
    }
    return self;
}



#pragma mark - createData

- (void)createData {
    [AfNetWorkingManager getInfoWithUrl:self.url block:^(NSDictionary *resultDic, NSError *error) {
        self.specialArr = [CRSpecialNewsTitleModel baseModelByArr:resultDic[self.specialID][@"topics"]];
        
        self.sava = [[CRSpecialSavaModel alloc] init];
        self.sava.titleArr = self.specialArr;
        self.sava.banner = resultDic[self.specialID][@"banner"];
        self.sava.sname = resultDic[self.specialID][@"sname"];
        
        [self createViews];
    }];
}



#pragma mark - createViews

- (void)createViews {
    
    self.headBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, (self.specialArr.count / 4) * 40 * HEIGHT / 667.0 + 80 * HEIGHT / 667.0)];
    [self.view addSubview:self.headBgView];
    
    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 80 * HEIGHT / 667.0)];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:_sava.banner] placeholderImage:nil];
    [self.headBgView addSubview:_headImageView];
    
    self.headTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 80 * HEIGHT / 667.0, WIDTH, (self.specialArr.count % 4 + 1) * 40 * HEIGHT / 667.0)];
    [self.headBgView addSubview:_headTitleView];
    
    NSInteger count = 0;
    NSInteger row = 0;
    for (NSInteger i = 0; i < self.specialArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(count * WIDTH / 4.0 + 5 * WIDTH / 375.0, row * 40 * HEIGHT / 667.0 + 10 * HEIGHT / 667.0, WIDTH / 4.0 - 10 * WIDTH / 375.0, 20 * HEIGHT / 667.0);
        [button setTitle:[self.specialArr[i] tname] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.tintColor = [UIColor blackColor];
        button.layer.borderWidth = 1;
        button.layer.borderColor = [[UIColor colorWithRed:0.659f green:0.659f blue:0.659f alpha:1.00f] CGColor];
        button.layer.cornerRadius = 3;
        button.tag = 1000 + i;
        [button addTarget:self action:@selector(jumpTitleAction:) forControlEvents:UIControlEventTouchUpInside];
        [_headTitleView addSubview:button];
        if (count == 3) {
            count = 0;
            row++;
        }
        else {
            count++;
        }
    }
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 * HEIGHT / 667.0, WIDTH, HEIGHT - 44 * HEIGHT / 667.0) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    
    _tableView.tableHeaderView = self.headBgView;
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [_tableView registerClass:[CRNewsTableViewCell class] forCellReuseIdentifier:@"nReuse"];
    [_tableView registerClass:[CRPicTableViewCell class] forCellReuseIdentifier:@"pnReuse"];
    
    [self.tableView reloadData];
}



#pragma mark - ViewMethods

- (void)viewWillAppear:(BOOL)animated {
    [self createData];
}

- (void)viewWillDisappear:(BOOL)animated {
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64 * HEIGHT / 667.0)];
    view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [backButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(15 * WIDTH / 375.0, 30, 24, 24);
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:backButton];
    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - tableViewDataSource & Delegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30 * HEIGHT / 667.0)];
    view.backgroundColor = [UIColor lightGrayColor];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = [NSString stringWithFormat:@"%ld/%ld %@", section + 1, self.specialArr.count, [self.specialArr[section] tname]];
    [view addSubview:label];
    label.frame = CGRectMake(0, 0, WIDTH, 30 * HEIGHT / 667.0);
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30 * HEIGHT / 667.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    CRSpecialNewsModel *specialNews = [self.specialArr[indexPath.section] docs][indexPath.row];
    
    if (specialNews.imgextra) {
        return 124 * HEIGHT / 667.0;
    }
    else {
        return 94 * HEIGHT / 667.0;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.specialArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.specialArr[section] docs].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CRSpecialNewsModel *specailNews = [self.specialArr[indexPath.section] docs][indexPath.row];
    
    if (specailNews.imgextra) {
        CRPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pnReuse" forIndexPath:indexPath];
        cell.specialNews = specailNews;
        return cell;
    }
    
    else {
        CRNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nReuse" forIndexPath:indexPath];
        cell.specialNews = specailNews;
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CRSpecialNewsModel *specailNews = [self.specialArr[indexPath.section] docs][indexPath.row];
    if (specailNews.imgextra) {
        
        NSString *firstStr = [specailNews.photosetID substringFromIndex:9];
        NSString *secondStr = [specailNews.photosetID substringWithRange:NSMakeRange(4, 4)];
        NSString *url = [NSString stringWithFormat:@"http://c.3g.163.com/photo/api/set/%@/%@.json", secondStr, firstStr];
        NSLog(@"%@", url);
        
        CRPicNewsViewController *pnVC = [[CRPicNewsViewController alloc] init];
        pnVC.url = url;
        pnVC.myTitle = specailNews.title;
        [self.navigationController pushViewController:pnVC animated:YES];
    }
    else {
        CRNewsDetailViewController *ndVC = [[CRNewsDetailViewController alloc] init];
        ndVC.str = specailNews.url;
        ndVC.myTitle = specailNews.title;
        [self.navigationController pushViewController:ndVC animated:YES];
    }
}



#pragma mark - ButtonAction

- (void)jumpTitleAction:(UIButton *)button {
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:button.tag - 1000] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (void)backAction:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
